package com.bobi.tfca.ui.mainnavigation.user.member

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Toast
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentMemberLeaveBinding
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.ui.adapter.MemberLeaveAdapter

class MemberLeaveFragment: BaseFragment<FragmentMemberLeaveBinding, MemberManageFragmentVM>(
    FragmentMemberLeaveBinding::inflate,
    MemberManageFragmentVM::class.java,
    true
) {
    private lateinit var memberRV: RecyclerView

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentMemberLeaveBinding,
        viewModel: MemberManageFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 初始化RV
        initRecyclerView(binding)

        // 监听viewmodel
        viewModel!!.apply {
            members.observe(viewLifecycleOwner) {
                val adapter = memberRV.adapter as MemberLeaveAdapter
                adapter.setItemList(it)
                adapter.notifyDataSetChanged()
            }
        }

        // 搜索查询
        binding.ivSearchIcon.setOnClickListener {
            val searchContent = binding.etSearchContent.text.toString()
            if (searchContent.isNullOrBlank()) {
                Toast.makeText(requireActivity(), "输入内容为空，请检查", Toast.LENGTH_SHORT).show()
            } else {
                viewModel.searchLeaveMemberByName(publicViewModel!!, searchContent,   this)
            }
        }
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentMemberLeaveBinding
    ) {
        // 获取RecyclerView实例
        memberRV = binding.rvMemberLeave

        // 获得所有活动
        val memberList : ArrayList<MemberResponse> = arrayListOf()
        // 设置布局管理器
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        memberRV.layoutManager = linearLayoutManager
        memberRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        val memberAdapter = MemberLeaveAdapter(this, memberList)

        // 设置监听器
        memberAdapter.setOnHeadsClickListener(object : MemberLeaveAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                // 设置跳转详情
                publicViewModel!!.member.value = memberAdapter.memberList[position]
                requireActivity().findNavController(R.id.app_nav)
                    .navigate(R.id.action_memberManageFragment_to_memberProfileFragment)
            }
        })
        memberAdapter.setOnLeaveWordsClickListener(object : MemberLeaveAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                // 设置跳转留言
                publicViewModel!!.member.value = memberAdapter.memberList[position]
                requireActivity().findNavController(R.id.app_nav)
                    .navigate(R.id.action_memberManageFragment_to_leaveWordsFragment)
            }
        })

        // 绑定Adapter
        memberRV.adapter = memberAdapter
    }

    override fun resumeFragment(viewModel: MemberManageFragmentVM?) {
        super.resumeFragment(viewModel)
        viewModel!!.findAllLeaveMembers(publicViewModel!!, this)
    }
}